/**
 * 
 */
package com.gr.staffpm;

import java.io.FileInputStream;
import java.util.Properties;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @author Graham Rhodes 25 Mar 2011 20:02:46
 */
public class StaffPMEntryPoint {

    private static Logger log = LoggerFactory.getLogger(StaffPMEntryPoint.class);

    public static void main(String[] args) {
        try {
            Properties prop = new Properties();
            prop.load(new FileInputStream(System.getProperty("STAFFPM_HOME") + "/etc/server.properties"));
            Server server = new Server(Integer.parseInt(prop.getProperty("staffpm.standalone.port", "9090")));

            String warFile = getBaseUrl();

            WebAppContext context = new WebAppContext();
            context.setDescriptor(warFile + "/WEB-INF/web.xml");
            context.setResourceBase(warFile);
            context.setContextPath("/");
            context.setParentLoaderPriority(true);

            server.setHandler(context);

            server.start();
            server.join();
        } catch (Exception e) {
            log.error("Exception starting webapp", e);
        }
    }

    public static String getBaseUrl() {
        if (System.getProperty("developmentMode") != null) {
            return "src/main/webapp";
        } else {
            String warFile = StaffPMEntryPoint.class.getClassLoader().getResource("WEB-INF").toExternalForm();
            return warFile.substring(0, warFile.lastIndexOf("/") + 1);
        }
    }

}
